const administratorAccountService = require("../service/administratorAccountService");
const config = require("../config");

const jwt = require("jsonwebtoken");

module.exports = {
    async verification(ctx, next) {
        const token = ctx.request.header.token;
        if (token) {
            const result = await administratorAccountService.getOneLogInfByToken(token);
            try {
                jwt.verify(result.token, config.secret);
                ctx.query.user = result;
                await next();
            } catch ({name, message, expiredAt}) {
                ctx.status = 401;
                ctx.body = {name, message, expiredAt};
            }
        } else {
            ctx.status = 401;
            ctx.body = {
                name: "TokenNotExits",
                message: "token not exists"
            };
        }
    }
};
